﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule;
using Entities = Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Acls;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Acl
{
	public partial class AclViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Entities.Acl> _acls;

		/// <summary>
		/// 
		/// </summary>
		private Entities.Acl _acl;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Entities.Acl> Acls
		{
			get
			{
				return _acls;
			}
			private set
			{
				_acls = value;
				if (_acls != null)
					RaisePropertyChanged("Acls");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Entities.Acl Acl
		{
			get
			{
				return _acl;
			}
			private set
			{
				_acl = value;
				if (_acl != null)
					RaisePropertyChanged("Acl");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private bool IsAclReady
		{
			get
			{
				return Acl != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadAcls()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IAclService aclService = IoCFactory.Resolve<IAclService>();
					e.Result = aclService.FindAllAcl();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Acls = e.Result as List<Entities.Acl>;
					else if (e.Error != null)
						MessageBox.Show(
							_aclView,
							e.Error.Message,
							WindowTitles.AclWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <param name="args"></param>
		private void SaveAcl(object s, EventArgs args)
		{
			if (_aclView.validator.Validate().IsValid)
			{
				StartWaiting();
				using (BackgroundWorker worker = new BackgroundWorker())
				{
					worker.DoWork += (sender, e) =>
					{
						IAclService aclService = IoCFactory.Resolve<IAclService>();
						if (Acl.ChangeTracker.State == ObjectState.Added)
							aclService.AddAcl(Acl);
						else
							aclService.ChangeAcl(Acl);
						Acl.AcceptChanges();
					};
					worker.RunWorkerCompleted += (sender, e) =>
					{
						EndWaiting();
						if (!e.Cancelled && e.Error == null)
							SetCurrentAcl(_aclView.aclBindingSource, args);
						else if (e.Error != null)
							MessageBox.Show(
								_aclView,
								e.Error.Message,
								WindowTitles.AclWindowTitle,
								MessageBoxButtons.OK,
								MessageBoxIcon.Error
							);
					};
					worker.WorkerSupportsCancellation = true;
					worker.RunWorkerAsync();
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewAcl(object sender, EventArgs e)
		{
			_aclView.aclBindingSource.AddingNew += AddingNewAcl;
			_aclView.aclBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewAcl(object sender, AddingNewEventArgs e)
		{
			Acl = new Entities.Acl
			{
				AclGuid = Guid.NewGuid(),
				AclId = 0,
				Descripcion = string.Empty,
				Nombre = string.Empty,
				Status = true,
			};
			Acl.MarkAsAdded();
			e.NewObject = Acl;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="s"></param>
		/// <param name="args"></param>
		private void DeleteAcl(object s, EventArgs args)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IAclService aclService = IoCFactory.Resolve<IAclService>();
					aclService.DeleteAcl(Acl);
					Acl.AcceptChanges();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						_aclView.aclBindingSource.RemoveCurrent();
					else if (e.Error != null)
						MessageBox.Show(
							_aclView,
							e.Error.Message,
							WindowTitles.AclWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentAcl(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Acl = bindingSource.Current as Entities.Acl;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnAclsPropertyChanged()
		{
			_aclView.aclBindingSource.DataSource = Acls;
			_aclView.aclBindingSource.CurrentChanged += SetCurrentAcl;
			SetCurrentAcl(_aclView.aclBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnAclPropertyChanged()
		{
			_aclView.saveButton.Enabled = _aclView.aclBindingNavigatorSaveItem.Enabled = IsAclReady;
		}

		#endregion

	}
}
